package org.webdb.webapp.jackson;

import java.io.IOException;
import java.util.Map.Entry;

import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.SerializerProvider;
import org.webdb.core.DocumentRecord;

public class DocumentRecordSerializer extends JsonSerializer<DocumentRecord> {

    @Override
    public void serialize(DocumentRecord value, JsonGenerator jgen,
            SerializerProvider provider) throws IOException,
            JsonProcessingException {
        jgen.writeStartObject();
        for (Entry<String, Object> entry : value.fieldEntrySet()) {
            jgen.writeFieldName(entry.getKey());
            provider.defaultSerializeValue(entry.getValue(), jgen);
        }
        jgen.writeEndObject();
    }

    @Override
    public Class<DocumentRecord> handledType() {
        return DocumentRecord.class;
    }
}
